package com.yjd.back.contentmanager;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.base.service.IIntroDetailService;
import com.yjd.comm.doctor.model.DoctorModel;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.fs.util.FileUtil;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.tag.service.ITagService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 后台医生相关操作
 * Created by yangchangyan on 2017/6/30.
 */
@Controller("com.yjd.api.doctor.DoctorControl")
@Scope("prototype")
@RequestMapping("/doctor")
public class DoctorControl extends BaseControl {

    @Resource
    private IDoctorService doctorService;

    @Resource
    private IIntroDetailService introDetailService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private ITagService tagService;

    /**
     * 查询所有医生，分页查询
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/all")
    public String allDoctor() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "create_time desc");
        paramMap.put("rows", "20");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.doctorService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/doctor/all"));

        DoctorModel dm = null;
        OrganizationModel org = null;
        for (Object obj : pager.getPagerModel().getDataList()) {
            dm = (DoctorModel) obj;
            org = organizationService.getModelById(dm.getOrg_id(), DbWREnums.READ);
            dm.getStrMap().put("org_name", null == org ? "" : org.getOrg_name());
            dm.getStrMap().put("position", TypeShowUtils.getDoctorTitleShow(dm.getPosition()));
            dm.getStrMap().put("create_time", DateUtil.timestampToDateString(dm.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            dm.getStrMap().put("last_update_time", DateUtil.timestampToDateString(dm.getLast_update_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);

        return "allDoctor";
    }

    /**
     * 发布新医生编辑页面
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add")
    public String addDoctorPage() throws Exception {
        request.setAttribute("tagModelList", tagService.queryTags(null, ReferenceTypeEnum.DOCTOR));
        request.setAttribute("orgids", organizationService.allOrgAndName());
        request.setAttribute("doctorTitle", TypeShowUtils.getDoctorTitle());
        return "addDoctor";
    }

    /**
     * 编辑页面
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/edit")
    public String editDoctorPage(Long id) throws Exception {
        DoctorModel dm = doctorService.getModelById(id, DbWREnums.READ);
        if(StringUtil.isNotEmpty(dm.getPortrait_path())){
            request.setAttribute("portrait_path", ServletUtil.getFileUrl(dm.getPortrait_path()));
        }
        IntroDetailModel idm = introDetailService.getModelById(dm.getDetail_id(), DbWREnums.READ);
        if (null != idm) {
            dm.getStrMap().put("detail", idm.getContent());
        }

        request.setAttribute("tagModelList", tagService.queryTags(id, ReferenceTypeEnum.DOCTOR));
        request.setAttribute("orgids", organizationService.allOrgAndName());
        request.setAttribute("doctorTitle", TypeShowUtils.getDoctorTitle());
        request.setAttribute("doctor", dm);
        return "addDoctor";
    }

    /**
     * 发布新医生
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add/submit")
    public void addDoctor(MultipartHttpServletRequest multipartRequest, DoctorModel dm) throws Exception {
        if (StringUtil.isEmpty(dm.getName())) {
            FrameUtil.throwBaseException1("医生名不能为空");
        }
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        // 上传医生头像
        ResourceModel res = new ResourceModel();
        res.setRef_type(ReferenceTypeEnum.DOCTOR.getCode());
        res.setRef_id(dm.getDoctor_id());
        String portrait_path = FileUtil.uploadFileToRemote(multipartRequest.getFileMap(), ReferenceTypeEnum.DOCTOR, res);
        if (StringUtil.isNotEmpty(portrait_path)) {
            dm.setPortrait_path(portrait_path);
        }

        doctorService.createOrUpdate(dm, (String) paramMap.get("detail"),request.getParameterValues("tags"));

        this.successMsg(ServletUtil.getUrl("/doctor/all"), "保存医生成功");
    }

    /**
     * 删除医生
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/delete")
    public void deleteDoctor(Long id) throws Exception {
        DoctorModel doctor = this.doctorService.getModelById(id, DbWREnums.READ);
        if (null == doctor) {
            FrameUtil.throwBaseException1("医生不存在");
        }

        introDetailService.delete(doctor.getDetail_id());
        doctorService.delete(doctor.getDoctor_id());

        this.successMsg("删除医生成功");
    }

    /**
     * 开启医生
     * @return void
     * @throws
     */
    @RequestMapping(value = "/open")
    public void openDoctor(Long id) throws Exception {
        changeDoctorIsUsed(id,1);
        this.successMsg("开启医生成功");
    }

    /**
     *禁用医生
     * @return void
     * @throws
     */
    @RequestMapping(value = "/close")
    public void closeDoctor(Long id) throws Exception {
        changeDoctorIsUsed(id,0);
        this.successMsg("禁用医生成功");
    }

    private void changeDoctorIsUsed(Long id,Integer isUsed)throws Exception{
        if(id==null||isUsed==null){
            FrameUtil.throwBaseException1("参数不能为空");
        }
        DoctorModel doctorModel = this.doctorService.getModelById(id, DbWREnums.READ);
        if(null == doctorModel){
            FrameUtil.throwBaseException1("医生不存在");
        }
        doctorService.updateMap(FrameUtil.newHashMap("doctor_id",id,"is_used",isUsed));
    }
}
